cap log close
snapshot erase _all
clear all
estimates drop _all

*===========================*
*	Set Globals				*
*===========================*

	global filenum 11
	
	global nonTW_samp 		1		// set to 0 or 1 to run on non-tw sample	
	
*===========================*
*	Set Locals				*
*===========================*
	
	#delimit ;
		local m_list 		"12 1 3 4 5 6";
		
		local keep_list 	"m12_x_small_y2020 m1_x_small_y2020 
							 m3_x_small_y2020 m4_x_small_y2020 m5_x_small_y2020 
							 m6_x_small_y2020";
						 
		local fixed_effect	"datevar#statefip "; // can add ind_2digit where appropriate;
		
		local outcomes 		"absent_paid";
		
		local sample_var	cdc_levels_m4;	// sample options: some_sick_leave, female, covid_low_m4 covid_detail childUnder5 gender_parent;
		
		local samp			"cdc_levels_m4"; 	// file name options: PSL, fem, apr_low covid_detail childUnder5 gender_parent;
			
	#delimit cr
	
*===========================*
*	Set File Name			*
*===========================*
	
		global filename 	"DDD_`samp'"
		
		if ${nonTW_samp}==1		global file_tag 	"TW0"
		if ${nonTW_samp}==0		global file_tag 	""
	
*===========================*
*	Load In Analysis File	*
*===========================*
	log using "${log_path}/${filenum}_${filename}_${file_tag}.smcl", replace
	
	use "${data_path}/analysis_file.dta" , clear
	
	if ${nonTW_samp} == 1		keep if tw_binary_10ly==0	
	
	sum inc_asec [w=wtfinl],d
	gen income_subsample = 1 	 if inc_asec<=r(p25)
	replace income_subsample = 2 if inc_asec>=r(p75)

		
	levelsof `sample_var', local(samp_levels)		
	
	snapshot save
	
*===================*
* 	DD in Time		*
* 	18/19 vs 20		*
*===================*


foreach v in `outcomes'{
	*	Setup Reg Results File	*
	clear
	save "${out_path}/${filenum}_`v'_`samp'_${file_tag}.dta", emptyok replace
	
	foreach x in `samp_levels'{
		
		snapshot restore 1
		
		keep if `sample_var'==`x'
		
		#delimit ;
		
			** NO CONTROLS **;
			reghdfe `v' 
					m12-m6 y2020 small
					small_x_y2020
					m12_x_y2020-m6_x_y2020
					m12_x_small-m6_x_small
					m12_x_small_y2020 - m6_x_small_y2020
					[w=wtfinl], a(`fixed_effect') vce(cluster cpsidp);
								
			test m12_x_small_y2020 = m1_x_small_y2020 = m3_x_small_y2020 = 0;
				scalar pval=`r(p)';					

			qui sum `v' if asec_year!=2020 & small==1 [w=wtfinl];
				scalar control_mean = `r(mean)';

			est sto ddd1_x`x';
			estadd local 	controls 	"NO";
			estadd local 	spec		"DDD";
			estadd local 	samp 		"`samp' `x'";
			estadd scalar 	cmean 		control_mean;
			estadd scalar 	parallel 	pval;	
			
			regsave `keep_list'
				using "${out_path}/${filenum}_`v'_`samp'_${file_tag}.dta",
				addlabel(outcome, "`v'", controls, "NO", `samp', "`x'", mu, `=control_mean') append;
			
			** WITH CONTROLS **;
			
			reghdfe `v' 
					m12-m6 y2020 small
					small_x_y2020
					m12_x_y2020-m6_x_y2020
					m12_x_small-m6_x_small
					m12_x_small_y2020 - m6_x_small_y2020
					bachelors age age2 childUnder12
					[w=wtfinl], a(`fixed_effect') vce(cluster cpsidp);
						
			test m12_x_small_y2020 = m1_x_small_y2020 = m3_x_small_y2020 = 0;
				scalar pval=`r(p)';					
							
			est sto ddd2_x`x';
			estadd local  	controls 	"YES";
			estadd local 	spec		"DDD";
			estadd local 	samp 		"`samp' `x'";
			estadd scalar 	cmean 		control_mean;
			estadd scalar 	parallel 	pval;	
			
		regsave `keep_list'
			using "${out_path}/${filenum}_`v'_`samp'_${file_tag}.dta",
			addlabel(outcome, "`v'", controls, "YES", `samp', "`x'", mu, `=control_mean') append ;
			
		#delimit cr
		
	} // end subsample loop
	
	#delimit ;
		esttab *
			using "${out_path}/${filenum}_`v'_`samp'_${file_tag}.csv",
			replace	label se star(* 0.1 ** .05 *** 0.01)
			s(cmean spec samp controls parallel N, 
				label("Control Mean" "Model" "Sample" "Controls" "Parallel Trends" "Observations"))
			keep(`keep_list');	
	
	#delimit cr
	
} // end outcomes loop
log close
